const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    keyword: '', 
    searched: false 
  },

  onInputChange(e) {
    this.setData({
      keyword: e.detail.value
    });
  },

   onSearch() {
    const keyword = this.data.keyword.trim();
    if (!keyword) {
      wx.showToast({
        title: '请输入关键词',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '搜索中'
    });

    db.collection('shangpinxinxi').where({
      title: db.RegExp({
        regexp: keyword,
        options: 'i',
      })
    }).get({
      success: (res) => {
        wx.hideLoading();
        this.setData({
          listData: res.data,
          searched: true
        });
        if (res.data.length === 0) {
          wx.showToast({
            title: '未找到相关商品',
            icon: 'none'
          });
        }
      },
    });
  },

  onLoad(options) {
    this.getdata()
  },
})